import tkinter
import pyautogui
import threading
import schedule
import time
import os
from tkinter import messagebox
import datetime

mainwin = tkinter.Tk()
mainwin.title('mouse')
mainwin.geometry('450x450')

# pyautogui.FAILSAFE = True
pyautogui.FAILSAFE = False

check_retry = 1
def do_click():
    try:
        x1 = x1_entry.get()
        check_x1 = str(x1)[0]
        y1 = y1_entry.get()
        check_y1 = str(y1)[0]
        x2 = x2_entry.get()
        check_x2 = str(x2)[0]
        y2 = y2_entry.get()
        check_y2 = str(y2)[0]
        x3 = x3_entry.get()
        check_x3 = str(x3)[0]
        y3 = y3_entry.get()
        check_y3 = str(y3)[0]
    except:
        messagebox.showerror(title='报错', message='获取坐标值失败，请检查输入的坐标值')
    try:
        # a = 1 / 0
        # print(a)
        global check_retry
        time.sleep(10)
        pyautogui.click(int(x1), int(y1))
        time.sleep(5)
        pyautogui.click(int(x2), int(y2))
        time.sleep(5)
        pyautogui.click(int(x3), int(y3))
        # clicks: 点击次数，默认1次。次数为2，等于一次双击操作
        # interval: 两次点击间隔时长，默认0.0；大于1.0后。默认为点击一次
        # duration: 所耗时长，默认0.0。
        check_retry = 1
    except Exception as ex:
        check_retry = 0
        ex = str(ex)
        # print(ex, type(ex))
        try:
            log_path = r'C:\Logs'
            if os.path.exists(log_path):
                pass
                # print('目标存在')
            else:
                os.makedirs(log_path)
                # print('创建完成')
            log_now = datetime.datetime.now()
            # print(log_now)
            lpg_strtime = datetime.datetime.strftime(log_now, "%Y/%m/%d %H:%M:%S")
            # print(lpg_strtime)
            log_strtime1 = lpg_strtime.split(' ')
            # print(log_strtime1)
            log_strtime2 = log_strtime1[0]
            log_timelist = log_strtime2.split('/')
            # print(log_timelist)
            log_year = log_timelist[0]
            log_month = log_timelist[1]
            log_day = log_timelist[2]
            # print(log_year)
            # print(log_month)
            # print(log_day)
            log_name = 'mouse' + str(log_year) + str(log_month) + str(log_day)
            # print(log_name)
            log_path_name = str(log_path) + '\\' + str(log_name) + '.txt'
            # print(log_path_name)
            f = open(log_path_name, "a")
            f.write(lpg_strtime)
            f.write('   ----')
            f.write(ex)
            f.write('\n')
            f.close()
            # messagebox.showerror(title='报错', message='执行点击失败')
        except:
            pass


def check_again():
    # 检查是否执行点击，当执行点击失败时，再次执行点击，循环10次，直到点击成功
    try:
        for i in range(10):
            if check_retry == 0:
                do_click()
                time.sleep(3)
            else:
                pass
    except:
        pass


def thread_manual_click():
    add_thread_manualclick = threading.Thread(target=do_click)
    add_thread_manualclick.start()


check_once = 2         # 检查自动执行只能开1次
def automatic_click():
    try:
        x1 = x1_entry.get()
        check_x1 = str(x1)[0]
        y1 = y1_entry.get()
        check_y1 = str(y1)[0]
        x2 = x2_entry.get()
        check_x2 = str(x2)[0]
        y2 = y2_entry.get()
        check_y2 = str(y2)[0]
        x3 = x3_entry.get()
        check_x3 = str(x3)[0]
        y3 = y3_entry.get()
        check_y3 = str(y3)[0]
        global check_once
        check_once -= 1
    except:
        messagebox.showerror(title='报错', message='获取坐标值失败')
    try:
        if check_once >= 1:
            do_show.config(text='自动执行中......', bg='lightgreen')
            # schedule.every(5).seconds.do(do_click)
            schedule.every().day.at('08:15').do(do_click)
            schedule.every().day.at('08:16').do(check_again)
            schedule.every().day.at('20:15').do(do_click)
            schedule.every().day.at('20:16').do(check_again)
            while True:
                schedule.run_pending()
                time.sleep(5)
        else:
            messagebox.showerror(title='报错', message='自动执行只能点一次')
    except:
        pass


def thread_automatic_click():
    add_thread_automaticclick = threading.Thread(target=automatic_click)
    add_thread_automaticclick.start()


def mouse_win():
    win = tkinter.Tk()
    win.geometry('500x300')
    win.title('get xy')

    def get_xy():
        xy = pyautogui.position()
        # print(xy)
        xy_label.config(text=f'{xy}')

    def get_xy2():
        schedule.every(5).seconds.do(get_xy)
        while True:
            schedule.run_pending()
            # time.sleep(5)

    def thread_get_xy():
        add_thread_getxy = threading.Thread(target=get_xy2)
        add_thread_getxy.start()

    get_title = tkinter.Label(win, text='点击<自动获取鼠标坐标>按钮后，        \n每5秒获取一次鼠标坐标，并更新到下方')
    get_title.place(x=11, y=11)
    xy_label = tkinter.Label(win, text='Point(x=   , y=   )', bg='lightgreen')
    xy_label.place(x=11, y=111)
    get_button = tkinter.Button(win, text='<自动获取鼠标坐标>', bg='skyblue', command=thread_get_xy)
    get_button.place(x=11, y=211)

    def quit_do():
        os._exit(0)

    win.protocol("WM_DELETE_WINDOW", quit_do)
    win.mainloop()


sleep1_title = tkinter.Label(mainwin, text='time.sleep(10)')
sleep1_title.place(x=33, y=33)
x1_title = tkinter.Label(mainwin, text='坐标1 x:')
x1_title.place(x=33, y=66)
x1_entry = tkinter.Entry(mainwin, width=10)
x1_entry.place(x=99, y=66)
y1_title = tkinter.Label(mainwin, text='坐标1 y:')
y1_title.place(x=233, y=66)
y1_entry = tkinter.Entry(mainwin, width=10)
y1_entry.place(x=299, y=66)
sleep2_title = tkinter.Label(mainwin, text='time.sleep(5)')
sleep2_title.place(x=33, y=99)

x2_title = tkinter.Label(mainwin, text='坐标2 x:')
x2_title.place(x=33, y=133)
x2_entry = tkinter.Entry(mainwin, width=10)
x2_entry.place(x=99, y=133)
y2_title = tkinter.Label(mainwin, text='坐标2 y:')
y2_title.place(x=233, y=133)
y2_entry = tkinter.Entry(mainwin, width=10)
y2_entry.place(x=299, y=133)
sleep2_title = tkinter.Label(mainwin, text='time.sleep(5)')
sleep2_title.place(x=33, y=166)

x3_title = tkinter.Label(mainwin, text='坐标3 x:')
x3_title.place(x=33, y=199)
x3_entry = tkinter.Entry(mainwin, width=10)
x3_entry.place(x=99, y=199)
y3_title = tkinter.Label(mainwin, text='坐标3 y:')
y3_title.place(x=233, y=199)
y3_entry = tkinter.Entry(mainwin, width=10)
y3_entry.place(x=299, y=199)

automatic_button = tkinter.Button(mainwin, text='每天8:15自动执行', bg='skyblue', command=thread_automatic_click)
automatic_button.place(x=33, y=250)
manual_button = tkinter.Button(mainwin, text='手动执行1次', bg='skyblue', command=thread_manual_click)
manual_button.place(x=290, y=250)
do_show = tkinter.Label(mainwin, text='自动执行未开始', width=43, bg='pink')
do_show.place(x=33, y=300)
mouse_button = tkinter.Button(mainwin, text='开启坐标获取工具', bg='skyblue', command=mouse_win)
mouse_button.place(x=33, y=350)


x1_entry.insert(0, 1878)
y1_entry.insert(0, 5)
x2_entry.insert(0, 1878)
y2_entry.insert(0, 5)
x3_entry.insert(0, 606)
y3_entry.insert(0, 488)



def quit_do2():
    os._exit(0)


mainwin.protocol("WM_DELETE_WINDOW", quit_do2)
mainwin.mainloop()
